package com.sjgtw.cloud.web.gridManage.controller;

import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.util.EasyExcelUtil;
import com.sjgtw.cloud.web.gridManage.domain.GridManage;
import com.sjgtw.cloud.web.gridManage.service.IGridManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 防洪网格化管理Controller
 * 
 * @author ruoyi
 * @date 2024-03-09
 */
@RestController
@Api(tags = "1.4、防洪网格化管理")
@RequestMapping("/gridManage")
public class GridManageController extends BaseController
{
    @Autowired
    private IGridManageService gridManageService;

    /**
     * 查询防洪网格化管理列表
     */
    //@RequiresPermissions("system:防洪网格化管理:list")
    @GetMapping("/list")
    @ApiOperation("列表")
    public TableDataInfo list(GridManage gridManage)
    {
        startPage();
        List<Map<String,Object>> list = gridManageService.selectGridManageList(gridManage);
        return getDataTable(list);
    }

    /**
     * 导出防洪网格化管理列表
     */
   // @RequiresPermissions("system:防洪网格化管理:export")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, GridManage gridManage)
//    {
//        List<GridManage> list = gridManageService.selectGridManageList(gridManage);
//        ExcelUtil<GridManage> util = new ExcelUtil<GridManage>(GridManage.class);
//        try {
//            util.exportExcel(response, list, "防洪网格化管理数据");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 获取防洪网格化管理详细信息
     */
    //@RequiresPermissions("system:防洪网格化管理:query")
    @GetMapping(value = "/getInfo/{id}")
    @ApiOperation("获取详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gridManageService.selectGridManageById(id));
    }

    /**
     * 新增防洪网格化管理
     */
    //@RequiresPermissions("system:防洪网格化管理:add")
    @PostMapping("/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody GridManage gridManage)
    {
        return toAjax(gridManageService.insertGridManage(gridManage));
    }

    /**
     * 修改防洪网格化管理
     */
    //@RequiresPermissions("system:防洪网格化管理:edit")
    @PutMapping("/edit")
    @ApiOperation("编辑")
    public AjaxResult edit(@RequestBody GridManage gridManage)
    {
        return toAjax(gridManageService.updateGridManage(gridManage));
    }

    /**
     * 删除防洪网格化管理
     */
   // @RequiresPermissions("system:防洪网格化管理:remove")
	@DeleteMapping("/delete/{id}")
    @ApiOperation("删除")
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(gridManageService.deleteGridManageById(id));
    }

    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, GridManage gridManage)
    {
        List<GridManage> list = gridManageService.selectGridManageList2(gridManage);
        EasyExcelUtil<GridManage> util = new EasyExcelUtil<>(GridManage.class);
        util.exportExcel(response, list, "防洪网格化管理", "防洪网格化管理", "gridManage.xlsx");
    }

    /**
     * 下载模版
     */
    @ApiOperation(value = "下载模版")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response)
    {
        EasyExcelUtil<GridManage> util = new EasyExcelUtil<>(GridManage.class);
        util.exportExcel(response, new ArrayList<>(), "防洪网格化管理", "防洪网格化管理模版", "gridManage.xlsx");
    }

    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        EasyExcelUtil<GridManage> util = new EasyExcelUtil<>(GridManage.class);
        List<GridManage> entities = util.readExcel(file, 0, 1);
        if (entities == null || entities.size() == 0) {
            return error("没有读取到数据");
        }

        List<String> errors = gridManageService.check(entities);
        if (errors.size() > 0) {
            return AjaxResult.error("", errors);
        }
        gridManageService.batchSave(entities);
        return success();
    }
}
